#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/8/28 11:08
# @Author : 闫峰
# @File    : promotions.py
import json

import requests

from third_services.WalMart.erp_get_token import get_token_by_erp


class Promotions:

    @staticmethod
    def get_promotions(ordersourcetype, OrderSourceId, sku):
        hearders = {
            "WM_QOS.CORRELATION_ID": "Irobotbox",
            "WM_SVC.NAME": "Walmart Marketplace",
            "Accept": "application/json",
            "Content-Type": "application/json",
            "WM_SEC.ACCESS_TOKEN": get_token_by_erp(ordersourcetype, OrderSourceId)
        }
        url = f"""https://marketplace.walmartapis.com/v3/promo/sku/{sku}"""
        res = requests.get(url=url, headers=hearders)
        if res.status_code == 200:
            return json.dumps(res.json(),indent=4)
        else:
            print(f"接口status_code！=200 res={res.text}")



    def create_report(ordersourcetype, OrderSourceId):
        hearders = {
            "WM_QOS.CORRELATION_ID": "Irobotbox",
            "WM_SVC.NAME": "Walmart Marketplace",
            "Accept": "application/json",
            "Content-Type": "application/json",
            "WM_SEC.ACCESS_TOKEN": get_token_by_erp(ordersourcetype, OrderSourceId)
        }
        url = f"""https://marketplace.walmartapis.com/v3/reports/reportRequests?reportType=SHIPPING_CONFIGURATION&reportVersion=v1"""
        data = {'rowFilters': None, 'excludeColumns': None}
        res = requests.post(url=url, headers=hearders,json=data)
        if res.status_code == 200:
            return json.dumps(res.json(),indent=4)
        else:
            print(f"接口status_code！=200 res={res.text}")
    @staticmethod
    def query_status(orderSourceType, OrderSourceId, requestId):
        url = f"https://marketplace.walmartapis.com/v3/reports/reportRequests/{requestId}"
        # param = {
        #     "reportType": "ITEM",
        #     "reportVersion": "v4"
        # }
        hearders = {
            "WM_QOS.CORRELATION_ID": "Irobotbox",
            "WM_SVC.NAME": "Walmart Marketplace",
            "Accept": "application/json",
            "WM_SEC.ACCESS_TOKEN": get_token_by_erp(orderSourceType, OrderSourceId)
        }

        res = requests.get(url=url, headers=hearders).json()
        print(res)
        print(json.dumps(res, indent=4))

    @staticmethod
    def download_report(orderSourceType, OrderSourceId, requestId):
        url = f"https://marketplace.walmartapis.com/v3/reports/downloadReport?requestId={requestId}"
        # param = {
        #     "reportType": "ITEM",
        #     "reportVersion": "v4"
        # }
        hearders = {
            "WM_QOS.CORRELATION_ID": "Irobotbox",
            "WM_SVC.NAME": "Walmart Marketplace",
            "Accept": "application/json",
            "WM_SEC.ACCESS_TOKEN": get_token_by_erp(orderSourceType, OrderSourceId)
        }

        res = requests.get(url=url, headers=hearders).json()
        print(res)
if __name__ == '__main__':
    # res = Promotions.create_report(45,20647)
    # res = Promotions.query_status(45,20647,"23f8bc3d-3f2d-4560-956e-f1200417cde8")
    res = Promotions.download_report(45,20647,"23f8bc3d-3f2d-4560-956e-f1200417cde8")
    print(res)